package com.xk.sport.backend.web.controller;

import com.google.common.base.Strings;
import com.xk.sport.backend.common.annotation.Login;
import com.xk.sport.backend.common.constant.ResultStatus;
import com.xk.sport.backend.common.entity.LessonQuery;
import com.xk.sport.backend.common.entity.Result;
import com.xk.sport.backend.common.util.XlsUtil;
import com.xk.sport.backend.dao.model.Lesson;
import com.xk.sport.backend.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Login
@Controller
@RequestMapping(value = "/lesson")
public class LessonController {
    @Autowired
    private LessonService lessonService;

    /**
     * 查询
     */
    @RequestMapping("/findAllByQueryAndPage")
    @ResponseBody
    public Result findAllByQueryAndPage(LessonQuery query) {
        Page<Lesson> page = lessonService.findAllByQuery(query);
        return new Result(ResultStatus.success.getStatus(), "", page);
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(){
        return new Result(ResultStatus.success.getStatus(), "" , lessonService.findAll());
    }

    /**
     * 查询
     */
    @RequestMapping("/findOne")
    @ResponseBody
    public Result findOne(long id) {
        Lesson lesson = lessonService.findOne(id);
        return new Result(ResultStatus.success.getStatus(), "", lesson);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Lesson lesson) {
        Lesson newLesson = lessonService.add(lesson);
        if (newLesson != null) {
            return new Result(ResultStatus.success.getStatus(), "", newLesson);
        } else {
            return new Result(ResultStatus.normal_error.getStatus(), "添加失败");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Lesson lesson) {
        Lesson newLesson = lessonService.update(lesson);
        if (newLesson != null) {
            return new Result(ResultStatus.success.getStatus(), "", newLesson);
        } else {
            return new Result(ResultStatus.normal_error.getStatus(), "修改失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(long id) {
        if (lessonService.delete(id)) {
            return new Result(ResultStatus.success.getStatus(), "");
        } else {
            return new Result(ResultStatus.normal_error.getStatus(), "删除失败");
        }
    }

    /**
     * 上传
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new Result(ResultStatus.normal_error.getStatus(), "文件为空");
        }
        String originalFileName = file.getOriginalFilename();
        if (Strings.isNullOrEmpty(originalFileName)) {
            return new Result(ResultStatus.normal_error.getStatus(), "文件名为空");
        }
        final List<Lesson> lessons = new ArrayList<>();
        int startrow = 0;
        int startcol = 0;
        XlsUtil.XlsErrorResult xlsErrorResult = XlsUtil.parseXls(file, startrow, startcol, (cellValue, i, j) -> {
            Lesson lesson = null;
            if (j == startcol) {
                lesson = new Lesson();
                lessons.add(lesson);
            } else {
                lesson = lessons.get(i - startrow);
            }
            cellValue = cellValue.trim();
            switch (j - startcol) {
                case 0:// 课程名对应name
                    lesson.setName(cellValue);
                    break;
                case 1:// 课程描述对应content
                    lesson.setContent(cellValue);
                    break;
            }
            return new XlsUtil.XlsErrorResult();
        });

        if (xlsErrorResult.hasError()) {
            return new Result(ResultStatus.normal_error.getStatus(), xlsErrorResult.toString());
        }

        for (Lesson lesson : lessons) {
            lessonService.add(lesson);
        }
        return new Result(ResultStatus.success.getStatus(), "");
    }
}
